| Conditions | 1 |
| Paths | 3 |
| Total Lines | 128 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* global API */ |
||
| 37 | function ($scope, $timeout, $location, $rootScope, StepsService, notify, HttpsTest) { |
||
| 38 | $scope.settings = { |
||
| 39 | nextcloud_host: '', |
||
| 40 | nextcloud_username: '', |
||
| 41 | nextcloud_password: '', |
||
| 42 | }; |
||
| 43 | |||
| 44 | |||
| 45 | $scope.vaults = []; |
||
| 46 | |||
| 47 | $scope.gogo = function (to) { |
||
| 48 | StepsService.steps().goTo(to); |
||
| 49 | }; |
||
| 50 | |||
| 51 | |||
| 52 | notify.config({ |
||
| 53 | 'position': 'left', |
||
| 54 | 'duration': 2500 |
||
| 55 | }); |
||
| 56 | |||
| 57 | $scope.check = { |
||
| 58 | server: function (callback) { |
||
| 59 | if (!$scope.settings.nextcloud_host || !$scope.settings.nextcloud_username || !$scope.settings.nextcloud_password) { |
||
| 60 | $scope.errors.push(API.i18n.getMessage('invalid_server_settings')); |
||
| 61 | callback(false); |
||
| 62 | return; |
||
| 63 | } |
||
| 64 | $scope.settings.nextcloud_host = $scope.settings.nextcloud_host.replace(/\/$/, ""); |
||
| 65 | PAPI.host = $scope.settings.nextcloud_host; |
||
| 66 | PAPI.username = $scope.settings.nextcloud_username; |
||
| 67 | PAPI.password = $scope.settings.nextcloud_password; |
||
| 68 | PAPI.getVaults(function (vaults) { |
||
| 69 | if (vaults.hasOwnProperty('error')) { |
||
| 70 | var errors = API.i18n.getMessage('invalid_response_from_server', [vaults.result.status, vaults.result.statusText]); |
||
| 71 | $scope.errors.push(errors); |
||
| 72 | notify(errors); |
||
| 73 | callback(false); |
||
| 74 | } |
||
| 75 | else { |
||
| 76 | $scope.vaults = vaults; |
||
| 77 | callback(true); |
||
| 78 | } |
||
| 79 | $scope.$apply(); |
||
| 80 | }); |
||
| 81 | }, |
||
| 82 | vault: function (callback) { |
||
| 83 | try { |
||
| 84 | PAPI.decryptString($scope.settings.default_vault.challenge_password, $scope.settings.vault_password); |
||
| 85 | callback(true); |
||
| 86 | } |
||
| 87 | catch (e) { |
||
| 88 | $scope.errors.push(); |
||
| 89 | notify(API.i18n.getMessage('invalid_vault_password')); |
||
| 90 | callback(false); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | }; |
||
| 94 | $scope.saving = false; |
||
| 95 | $scope.next = function () { |
||
| 96 | $scope.saving = true; |
||
| 97 | $scope.errors = []; |
||
| 98 | $timeout(function () { |
||
| 99 | var step = StepsService.getCurrent().name; |
||
| 100 | var check = $scope.check[step]; |
||
| 101 | if (typeof check === "function") { |
||
| 102 | check(function (result) { |
||
| 103 | $scope.saving = false; |
||
| 104 | if (result) { |
||
| 105 | $scope.errors = []; |
||
| 106 | $scope.$apply(); |
||
| 107 | StepsService.steps().next(); |
||
| 108 | } |
||
| 109 | $timeout(function () { |
||
| 110 | $scope.errors = []; |
||
| 111 | $scope.$apply(); |
||
| 112 | }, 5000); |
||
| 113 | }); |
||
| 114 | } |
||
| 115 | else { |
||
| 116 | $scope.saving = false; |
||
| 117 | StepsService.steps().next(); |
||
| 118 | } |
||
| 119 | }, 10); |
||
| 120 | }; |
||
| 121 | |||
| 122 | var handleCheck = function (resultUrl) { |
||
| 123 | $scope.settings.nextcloud_host = resultUrl; |
||
| 124 | }; |
||
| 125 | |||
| 126 | $scope.isHTTP = function (url) { |
||
| 127 | return HttpsTest.isHTTP(url); |
||
| 128 | }; |
||
| 129 | |||
| 130 | $scope.checkHost = function () { |
||
| 131 | HttpsTest.test($scope.settings.nextcloud_host).then(handleCheck, handleCheck); |
||
| 132 | }; |
||
| 133 | |||
| 134 | $scope.cancelAdd = function () { |
||
| 135 | window.location = '#!/settings/2'; |
||
| 136 | }; |
||
| 137 | |||
| 138 | $scope.finished = function () { |
||
| 139 | var _settings = angular.copy($scope.settings); |
||
| 140 | |||
| 141 | var account = { |
||
| 142 | nextcloud_host: _settings.nextcloud_host, |
||
| 143 | nextcloud_username: _settings.nextcloud_username, |
||
| 144 | nextcloud_password: _settings.nextcloud_password, |
||
| 145 | vault: _settings.default_vault, |
||
| 146 | vault_password: _settings.vault_password |
||
| 147 | }; |
||
| 148 | $scope.saving = true; |
||
| 149 | API.runtime.sendMessage(API.runtime.id, {'method': 'getRuntimeSettings'}).then(function (settings) { |
||
| 150 | settings.accounts.push(account); |
||
| 151 | API.runtime.sendMessage(API.runtime.id, { |
||
| 152 | method: "saveSettings", |
||
| 153 | args: settings |
||
| 154 | }).then(function () { |
||
| 155 | setTimeout(function () { |
||
| 156 | notify(API.i18n.getMessage('account_added')); |
||
| 157 | $scope.saving = false; |
||
| 158 | window.location = '#!/settings/2'; |
||
| 159 | }, 750); |
||
| 160 | }); |
||
| 161 | |||
| 162 | }); |
||
| 163 | }; |
||
| 164 | }]); |
||
| 165 | }()); |
||
| 167 |